package cart.api;

import cart.api.vo.*;
import cart.model.order.OrderMain;
import cart.model.order.SubOrder;
import cart.model.payment.PaymentReturnVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import utils.sql.PageVo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Roney on 2016/11/3.
 */
public interface OrderMainApi {
    OrderMain findOrderByOrderNo(String s);

    void save(OrderMain orderMain);

    List<OrderMain> findByOrderPriceIsNull();

    public static enum CancelOrderType {
        USER, // 客户主动取消订单
        ERROR, // 处理出错系统取消订单
        OVERTIMED, // 等待付款超时系统取消订单
        OTHER // 其它原因（预留）
    };

    /**
     * 热销商品数据
     *
     * @return
     */
    public Map getHotSaleGoodsPage(String categoryId, int page, int size);

    /**
     * 更新或新增订单
     *
     * @param order
     * @return
     */
    public OrderMain saveOrUpdateOrder(OrderMain order);

    public OrderMain findById(String id);

    public OrderMain findByOrderNo(String orderNo);

    public List<OrderMain> findAllByUserId(String userId);

    public List<OrderMain> findAllByMemberId(String memberId);

    /**
     * 确认订单（第三方）
     * 调用该方法后，直接与第三方平台确认订单（真实下单）
     * @param orderNo
     */
    public void confirmOrder(String orderNo);

    /**
     * 取消订单
     * @param orderNo 订单号
     * @param cancelType 取消原因类型
     * @see CancelOrderType
     *
     * 调用样例：
     * 1. 客户主动取消订单（会员中心展示已取消状态）
     *      orderMainApi.cancelOrder("2017000001", CancelOrderType.USER)
     * 2. 系统异常取消订单（用于下单失败、第三方订单提交失败等异常流）
     *      orderMainApi.cancelOrder("2017000001", CancelOrderType.ERROR)
     */
    public void cancelOrder(String orderNo, CancelOrderType cancelType);

    public void prePayOrder(String orderNo);

    public String toString();

    /**
     * 生成订单
     *
     * @param tempOrderVo // 购物车对象
     * @param addressId      // 配送地址ID
     * @param usrId          // CoreUser ID
     * @param memberId       // Member ID
     * @param payMethodCode  // 支付方式代码
     * @return [
     * status: success-- 成功， error-- 失败,
     * error: 0-- 成功，1-- 处理失败， 2-- 1个以上订单生成失败
     * data: [
     * resOrderList: 生成的订单对象列表,
     * resDetails:[[orderId: <订单ID>, error:<0--成功，1--出错>, message:<错误信息>]...] 处理结果
     * ]
     * ]
     */
    public Map<String, Object> createOrder(TempOrderVo tempOrderVo,
                                           String addressId, String usrId, String memberId, String payMethodCode);

    /**
     * 生成订单
     *
     * @param tempOrderVo // 购物车对象
     * @param addressId      // 配送地址ID
     * @param usrId          // CoreUser ID
     * @param memberId       // Member ID
     * @param payMethodCode  // 支付方式代码
     * @param invoiceId      // MemberInVoice.id 发票信息ID
     * @return [
     * status: success-- 成功， error-- 失败,
     * error: 0-- 成功，1-- 处理失败， 2-- 1个以上订单生成失败
     * data: [
     * resOrderList: 生成的订单对象列表,
     * resDetails:[[orderId: <订单ID>, error:<0--成功，1--出错>, message:<错误信息>]...] 处理结果
     * ]
     * ]
     */
    public Map<String, Object> createOrder(TempOrderVo tempOrderVo,
                                           String addressId, String usrId, String memberId, String payMethodCode, String invoiceId);

    //收款管理分页查询订单
    public PageVo findReceiptOrderInfo(Pageable pageable, Map<String, Object> receiptQueryVo);

    //订单管理查询API分页的
    public PageVo findOrderInfo(Pageable pageable, Map<String, Object> orderQueryVo);

    //订单管理查询API不分页的
    public List<Map<String, Object>> findOrderInfo(Map<String, Object> orderQueryVo);

    //查询订单的所有退款明细
    public List<Map<String, Object>> findReturnInfo(Map<String,Object> orderQueryVo);

    //保存订单
    public OrderMain saveOrder(OrderMain orderMain);

    /**
     * 发票管理 更新订单信息
     *
     * @param orderNos
     * @param invoiceIds
     * @param tradeNos
     * @return
     */
    public Map updateOrderList(String[] orderNos, String[] invoiceIds, String[] tradeNos);

    /**
     * 根据订单号查询订单
     *
     * @param orderNos
     * @return
     */
    public Map findOrderListByOrderNos(String[] orderNos);

    public Page<OrderMain> findByOrganizationId(String organizationId, Pageable pageable);

    public Page<OrderMain> findOrdersByOrganization(String organizationId, Date startShippingTime, Date endShippingTime, List<String> status, List<String> memberIds, String orderNo, Pageable pageable);

    public Page<OrderMain> findOrdersByAllOrganization(Date startShippingTime, Date endShippingTime, List<String> status, List<String> memberIds, String orderNo, Pageable pageable);

    public List<OrderMain> findOrdersByOrganization(String organizationId, Date startDateCreated, Date endDateCreated, List<String> status, List<String> memberIds, String orderNo);

    public Page<OrderMain> findByExamplePaged(OrderMain example, Pageable pageable);

    Map<String, Object> createScoreOrder(TempOrderVo tempOrderVo, String id, String id1, String id2, String payMethodCode);

    /**
     * 查找需操作发票的订单
     * @param queryVo
     * @param pageable
     * @return
     */
    public Page<OrderMain> findByQueryVoPaged(OrderQueryVo queryVo, Pageable pageable);

    /**
     * 支付回调确认
     *
     * @param paymentReturnVo
     * @return 返回“success" 时为成功，其它为错误信息
     */
    public String payConfirm(PaymentReturnVo paymentReturnVo);

    //订单列表
    public JSONObject findUserCenterOrders(String orderNo, List<String> status, String memberId, Date startDate,Date endDate, Pageable pageable);
    //订单详情
    public JSONObject findOrderDetail(String orderNo,String memberId);


	/**
     * 关联第三方订单编号
     *
     * @param id
     * @param thirdOrderNo
     */
    public void linkThirdOrderNo(String id,String thirdOrderNo);

    int deleteSubOrdersByOrderMainId(String orderMainId);

    void deleteSubOrders(List<SubOrder> subOrder);

    Map<String, Object> occupyStock(OrderMain orderMain);

    Map<String, Object> cancelOccupyStock(OrderMain orderMain);

    Map<String, Object> confirmOccupyStock(OrderMain orderMain);

    /**
     * 查询运费（临时订单）
     * @param storeId : String
     * @param tempOrderItemVos : 临时订单项目
     * @param addressId : MemberAddress表ID
     * @return
     */
    public BigDecimal queryShippingFee(String storeId, List<TempOrderItemVo> tempOrderItemVos, String addressId);

    /**
     * 查询运费（购物车）
     * @param storeId : String
     * @param cartItemVoList : 临时订单项目
     * @param addressId : MemberAddress表ID
     * @return
     */
    public BigDecimal queryCartShippingFee(String storeId, List<CartItemVo> cartItemVoList, String addressId);


    JSONObject findDisscussByOrderNo(String orderNo);

    JSONObject findOrderItemsPageByOrderNo(Pageable pageable, String orderNo);

    //处理被拒绝的订单，返回积分给个会员
    public void dealWithRefuseOrder();

    /**
     * 使用productFashionId列表及数量生成临时订单对象
     * @param fashionIdAndCountVoList fashionId及购买数量VO列表
     * @return TempOrderVo 临时订单
     */
    public TempOrderVo newTempOrderByFasions(List<FashionIdAndCountVo> fashionIdAndCountVoList, String organizationCode);

    /**
     * 使用购物车记录生成临时订单对象
     * @param cartItemIds
     * @param userId
     * @return
     */
    public TempOrderVo newTempOrderByCartItems(List<String> cartItemIds, String userId, String organizationCode);

    String generateOrderNo();

    void splitOrderAll();

    void splitOrderById(String id);

    /**
     * 查询超时未支付订单
     * @param date
     * @param statusList
     * @return
     */
    List<OrderMain> listOverTimeUnPayOrders(Date date ,List<String> statusList);

    /**
     * 查询超时未支付且未发送信息的订单
     * @param date
     * @param statusList
     * @return
     */
    List<OrderMain> listOverTimeUnSendSmsAndUnPayOrders(Date date ,List<String> statusList,String sendSms);

    List<OrderMain> saveOrderMainList(List<OrderMain> orderMains);
}
